﻿// MoveCircle.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "MoveCircle.h"
#include "Data.h"


// MoveCircle 对话框

IMPLEMENT_DYNAMIC(MoveCircle, CDialogEx)

MoveCircle::MoveCircle(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MoveCircle, pParent)
{

}

MoveCircle::~MoveCircle()
{
}

void MoveCircle::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(MoveCircle, CDialogEx)
	ON_BN_CLICKED(IDC_Start, &MoveCircle::OnBnClickedStart)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// MoveCircle 消息处理程序

//启动
void MoveCircle::OnBnClickedStart()
{
	// TODO: 在此添加控件通知处理程序代码
	//获取窗口数据
	CString speed, accel, decel, radio, xPointEnd, yPointEnd, xPointMid, YPointMid;
	int dir;
	GetDlgItem(IDC_Speed_2)->GetWindowTextW(speed);
	GetDlgItem(IDC_Accel_2)->GetWindowTextW(accel);
	GetDlgItem(IDC_Decel_2)->GetWindowTextW(decel);
	GetDlgItem(IDC_Radio)->GetWindowTextW(radio);
	GetDlgItem(IDC_XPointEnd)->GetWindowTextW(xPointEnd);
	GetDlgItem(IDC_YPointEnd)->GetWindowTextW(yPointEnd);
	GetDlgItem(IDC_XPointMid)->GetWindowTextW(xPointMid);
	GetDlgItem(IDC_YPointMid)->GetWindowTextW(YPointMid);
	dir = ((CButton*)GetDlgItem(IDC_Dir0))->GetCheck() ? 1 : 0;	//顺逆

	if (_ttof(xPointMid) >= _ttof(xPointEnd) || _ttof(YPointMid) >= _ttof(yPointEnd)) {
		MessageBox(_T("圆弧半径应小于终点"));
		return;
	}

	//运动
	PMC_Base(Data::handle, 2, Data::axisList);
	int enable = 0;
	for (size_t i = 0; i < 2; i++)
	{
		PMC_GetAxisEnable(Data::handle, i, &enable);
		if (enable == 0)
		{
			MessageBox(_T("请检查轴0-1使能情况！！！"));
			return;
		}
		PMC_SetAtype(Data::handle, i, 1);
		PMC_SetUnits(Data::handle, i, 100);
		PMC_SetSpeed(Data::handle, i, _ttof(speed));
		PMC_SetAccel(Data::handle, i, _ttof(accel));
		PMC_SetDecel(Data::handle, i, _ttof(decel));
	}
	if (((CButton*)GetDlgItem(IDC_Mode0))->GetCheck())		//绝对
	{
		if (((CButton*)GetDlgItem(IDC_Type0))->GetCheck())	PMC_MoveCircAbs(Data::handle, 2, Data::axisList, _ttof(xPointEnd), _ttof(yPointEnd), _ttof(xPointMid), _ttof(YPointMid), dir);
		else if (((CButton*)GetDlgItem(IDC_Type1))->GetCheck())		PMC_MoveCirc2Abs(Data::handle, 2, Data::axisList, _ttof(xPointMid), _ttof(YPointMid), _ttof(xPointEnd), _ttof(yPointEnd));
		else		PMC_MoveCirc3Abs(Data::handle, 2, Data::axisList, _ttof(xPointEnd), _ttof(yPointEnd), _ttof(radio), dir);
	}
	else		//绝对
	{
		if (((CButton*)GetDlgItem(IDC_Type0))->GetCheck())	PMC_MoveCirc(Data::handle, 2, Data::axisList, _ttof(xPointEnd), _ttof(yPointEnd), _ttof(xPointMid), _ttof(YPointMid), dir);
		else if (((CButton*)GetDlgItem(IDC_Type1))->GetCheck())		PMC_MoveCirc2(Data::handle, 2, Data::axisList, _ttof(xPointMid), _ttof(YPointMid), _ttof(xPointEnd), _ttof(yPointEnd));
		else		PMC_MoveCirc3(Data::handle, 2, Data::axisList, _ttof(xPointEnd), _ttof(yPointEnd), _ttof(radio), dir);
	}
}


void MoveCircle::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (Data::CardIsConnect())
	{
		if (Data::runFlag())
		{
			GetDlgItem(IDC_Start)->EnableWindow(false);
		}
		else
		{
			GetDlgItem(IDC_Start)->EnableWindow(true);
		}
	}
	CDialogEx::OnTimer(nIDEvent);
}

//初始化
BOOL MoveCircle::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	((CButton*)GetDlgItem(IDC_Mode0))->SetCheck(true);
	((CButton*)GetDlgItem(IDC_Type0))->SetCheck(true);
	((CButton*)GetDlgItem(IDC_Dir0))->SetCheck(true);
	GetDlgItem(IDC_Speed_2)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Accel_2)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Decel_2)->SetWindowTextW(_T("1000"));
	//运动点位
	GetDlgItem(IDC_XPointEnd)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_YPointEnd)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_XPointMid)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_YPointMid)->SetWindowTextW(_T("50"));
	//半径
	GetDlgItem(IDC_Radio)->SetWindowTextW(_T("100"));
	SetTimer(1, 200, NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
